package com.itheima.controller;


import com.itheima.pojo.Blog;
import com.itheima.pojo.Link;
import com.itheima.pojo.WebConfig;
import com.itheima.service.BlogService;
import com.itheima.service.BlogTagService;
import com.itheima.service.LinkService;
import com.itheima.service.WebConfigService;
import com.itheima.utils.MessageConstant;
import com.itheima.utils.ResultUtil;
import com.itheima.utils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/index")
@Api(tags = "首页相关接口")
@CrossOrigin
public class IndexController {
    @Autowired
    private LinkService linkService;

    @Autowired
    private BlogService blogService;

    @Autowired
    private BlogTagService blogTagService;

    @Autowired
    private WebConfigService webConfigService;

    @GetMapping("/addLinkCount")
    @ApiOperation("增加友情链接点击数")
    public String addLinkCount(String uid){
        try {
            linkService.addLinkCount(uid);
            return ResultUtil.result(200, MessageConstant.INDEX_ADDLINKCOUNT_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.result(401,MessageConstant.INDEX_ADDLINKCOUNT_FAIL);
        }
    }

    @GetMapping("/getLink")
    @ApiOperation("获取友情链接")
    public String getLink(){
        try {
            List<Link> list = linkService.getLink();
            return ResultUtil.result(200,list);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.result(401,MessageConstant.INDEX_GETLINK_FAIL);
        }
    }

    @GetMapping("/getBlogByLevel")
    @ApiOperation("通过推荐等级获取博客列表")
    public String getBlogByLevel(Integer level, Integer currentPage, Integer useSort){
        try {
            List<Blog> list = blogService.getBlogByLevel(level, currentPage, useSort);
            return  ResultUtil.result(200,level);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.result(401, MessageConstant.INDEX_GETBLOGBYLEVEL_FAIL);
        }
    }

    @GetMapping("/getBlogByTime")
    @ApiOperation("按时间戳获取博客")
    public String getBlogByTime(Integer currentPage, Integer pageSize, String startDate, String endDate){
        try {
            if(StringUtils.isEmpty(startDate)||StringUtils.isEmpty(endDate)){
                return ResultUtil.result(401,MessageConstant.INDEX_GETBLOGBYTIME_FAIL);
            }
            List<Blog> list = blogService.getBlogByTime(currentPage, pageSize, startDate, endDate);
            return ResultUtil.result(200,list);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.result(401, MessageConstant.INDEX_GETBLOGBYTIME_FAIL);
        }
    }

    @GetMapping("/getHotBlog")
    @ApiOperation("获取首页排行博客")
    public String getHotBlog(){
        try {
            List<Blog> blogList = blogService.getHotBlog();
            return ResultUtil.result(200,blogList);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.result(401, MessageConstant.INDEX_GETHOTBLOG_FAIL);
        }
    }

    @GetMapping("/getNewBlog")
    @ApiOperation("获取首页最新的博客")
    public String getNewBlog(Integer curretnPage,Integer pageSize){
        try {
            List<Blog> blogList = blogService.getNewBlog(curretnPage,pageSize);
            return ResultUtil.result(200,blogList);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.result(401, MessageConstant.INDEX_GETNEWBLOG_FAIL);
        }
    }

    @GetMapping("/getHotTag")
    @ApiOperation("获取最热标签")
    public String getHotTag(){
        try {

            List<Blog> list = blogTagService.getHotTag();
            return ResultUtil.result(200, list);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.result(401, MessageConstant.INDEX_GETHOTTAG_FAIL);
        }
    }

    @GetMapping("/getWebConfig")
    @ApiOperation("获取网站配置")
    public String getWebConfig(){
        try {
            List<WebConfig> list = webConfigService.getWebConfig();
            return ResultUtil.result(200,list);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.result(401, MessageConstant.INDEX_GETWEBCONFIG_FAIL);

        }
    }
}
